import 'package:flutter/material.dart';
import 'package:flutter_app/bean/News.dart';
import 'package:dio/dio.dart';
import 'dart:convert';
import 'package:flutter_app/bean/RegionRes.dart';
import 'package:flutter_app/utils/windowUtil.dart';
import 'package:flutter/services.dart';
import 'dart:async'; // 异步
import 'package:flutter_app/widget/AppBarTemp1.dart';
import 'package:flutter_app/widget/LoadingDialog.dart';
//import 'package:fluttertoast/fluttertoast.dart';
import 'dart:io';
class Member2 extends StatefulWidget {
  @override
  State<Member2> createState() => _Member2State();
}

class _Member2State extends State<Member2> with AutomaticKeepAliveClientMixin {
  List<Item> list;
  var platform = MethodChannel("samples.flutter.io/battery");
  String _batteryLevel = "Unknown battery level.";
 bool show=true;
  @override
  bool get wantKeepAlive => true;
  @override
  void initState() {
    super.initState();
    list = List();
  }

  @override
  Widget build(BuildContext context) {
    new Future((){
      if(show){
        showDialog(context: context,
            builder: (bui){

                return LoadingDialog(text: "正在加载");


            }
        );
        getHttp(context);
      }


    });

    var httpClient = new HttpClient();
    return Scaffold(
      appBar: AppBarTemp1(
        child: Container(
          margin:
              EdgeInsets.only(left: 10.0, top: 10.0, bottom: 10.0, right: 10.0),
          child: Row(
            mainAxisAlignment: MainAxisAlignment.spaceBetween,
            children: <Widget>[
              Text(
                "个人中心",
                style: TextStyle(
                    fontSize: 18.0,
                    color: Color(0xff121212),
                    fontWeight: FontWeight.bold),
              ),
              Container(
                width: 103.0,
                height: 35.0,
                decoration: BoxDecoration(
                    borderRadius: BorderRadius.circular(20.0),
                    border: Border.all(color: Color(0xffededed))),
                child: Row(
                  children: <Widget>[
                    Container(
                      width: 50.0,
                      height: 40.0,
                      child: Image.asset("images/menu.png"),
                    ),
                    Container(
                      height: 30.0,
                      width: 1.0,
                      color: Color(0xffededed),
                    ),
                    Container(
                      width: 50.0,
                      height: 40.0,
                      padding: EdgeInsets.only(
                          top: 10.0, bottom: 10.0, left: 5.0, right: 5.0),
                      child: Image.asset(
                        "images/round.png",
                        width: 20.0,
                        height: 20.0,
                      ),
                    )
                  ],
                ),
              )
            ],
          ),
        ),
      ),
      body:
      hasResult(),
//      httpClient.getUrl(Uri.parse('http://cache.video.iqiyi.com/jp/avlist/202861101/1/?callback=jsonp9'))
//         FutureBuilder<List<Item>>(
//    future: getHttp(context),
//    builder: (context, snap) {
//    if (snap.connectionState == ConnectionState.done) {
//    if (snap.hasError) {
//    return Text("e");
//    }
//
////    Map news = json.decode(snap.data.toString());
////    RegionRes regionRes = new RegionRes.fromJson(news);
////    // print(response);
////    print(regionRes.result[0].path);
////    list.addAll(regionRes.result);
//    setState(() {
//
//    });
//    hasResult();
//    } else if (snap.connectionState == ConnectionState.active) {
//    return Text("w");
//    } else if (snap.connectionState == ConnectionState.waiting) {
//    return Text("w");
//    } else if (snap.connectionState == ConnectionState.none) {
//    return Text("");
//    }
//    },
//    ),
      backgroundColor: Color(0xffF2F2F2),
    );
  }

  Future<List<Item>> getHttp(BuildContext context) async {
//    showDialog<Null>(
//        context: context, //BuildContext对象
//        barrierDismissible: false,
//        builder: (BuildContext context) {
//          return new LoadingDialog(
//            //调用对话框
//            text: '正在获取数据...',
//          );
//        });
    try {
      Dio dio = Dio();

      dio.options = new BaseOptions(responseType: ResponseType.plain,connectTimeout: 15000);
      Response response =
          await dio.get("https://api.apiopen.top/getWangYiNews");
      Map news = json.decode(response.data.toString());
      RegionRes regionRes = new RegionRes.fromJson(news);
      // print(response);
      print(regionRes.result[0].path);
      list.addAll(regionRes.result);

        Navigator.pop(context);
      show=false;
     // Navigator.pop(context);
      setState(() {});
    } catch (e) {
      print(e);
      //  Navigator.pop(context);
    }
  }
  Future<String> getHttp1(BuildContext context) async {
//    showDialog<Null>(
//        context: context, //BuildContext对象
//        barrierDismissible: false,
//        builder: (BuildContext context) {
//          return new LoadingDialog(
//            //调用对话框
//            text: '正在获取数据...',
//          );
//        });
    try {
      Dio dio = Dio();

      dio.options = new BaseOptions(responseType: ResponseType.plain,connectTimeout: 15000);
      Response response =
          await dio.get("https://api.apiopen.top/getWangYiNews");
//      Map news = json.decode(response.data.toString());
//      RegionRes regionRes = new RegionRes.fromJson(news);
//      // print(response);
//      print(regionRes.result[0].path);
//      list.addAll(regionRes.result);
//      //  Navigator.pop(context);
//      setState(() {});
//    return response.data.toString();
    } catch (e) {
      print(e);
      //  Navigator.pop(context);
    }
  }

  Widget buildText(indext) {
    return Container(
        width: windowUtil.width,
        color: Color(0xffffffff),
        padding: EdgeInsets.only(top: 10.0, left: 10.0),
        child: Column(
          children: <Widget>[
            Row(
              crossAxisAlignment: CrossAxisAlignment.center,
              children: <Widget>[
                Image.network(
                  list[indext].image,
                  width: 35.0,
                  height: 35.0,
                  fit: BoxFit.fill,
                ),
                SizedBox(
                  width: 10.0,
                ),
                Expanded(
                    child: Column(
                  children: <Widget>[
                    Row(
                      crossAxisAlignment: CrossAxisAlignment.center,
                      children: <Widget>[
                        Expanded(
                          child: Text(
                            "${list[indext].title}",
                            overflow: TextOverflow.ellipsis,
                            maxLines: 1,
                          ),
                        ),
                        Container(
                          width: 15.0,
                          height: 15.0,
                          child: Image.asset("images/arr.png"),
                        ),
                      ],
                    ),
                  ],
                )),
                SizedBox(
                  width: 10.0,
                )
              ],
            ),
            Container(
              color: Color(0xffededed),
              height: 0.5,
              margin: EdgeInsets.only(top: 10.0, left: 45.0),
            )
          ],
        ));
  }

  Future<Null> _getBatteryLevel() async {
    String batteryLevel;
    try {
      print('dart-_getBatteryLevel');
      // 在通道上调用此方法
      final int result = await platform.invokeMethod('getBatteryLevel');
      batteryLevel = 'Battery level at $result % .';
    } on PlatformException catch (e) {
      batteryLevel = "Failed to get battery level: '${e.message}'.";
    }
    setState(() {
      _batteryLevel = batteryLevel;
    });
  }

  Widget hasResult() {
    return CustomScrollView(
      slivers: <Widget>[
        SliverToBoxAdapter(
          child: Container(
            color: Color(0xffffffff),
            child: Column(
              children: <Widget>[
                SizedBox(
                  height: 20.0,
                ),
                Container(
                  margin: EdgeInsets.only(top: 50.0),
                  width: 50.0,
                  height: 50.0,
                  child: new ClipOval(
                    child: new Image.asset("images/av1.jpg"),
                  ),
                ),
                Text("二狗"),
                Container(
                  color: Color(0xffeeeeee),
                  height: 10.0,
                ),
                GestureDetector(
                  child: Container(
                      width: windowUtil.width,
                      color: Color(0xffffffff),
                      padding:
                          EdgeInsets.only(top: 10.0, bottom: 10.0, left: 10.0),
                      child: Column(
                        children: <Widget>[
                          Row(
                            crossAxisAlignment: CrossAxisAlignment.center,
                            children: <Widget>[
                              Image.asset(
                                "images/share.png",
                                width: 35.0,
                                height: 35.0,
                                fit: BoxFit.fill,
                              ),
                              SizedBox(
                                width: 10.0,
                              ),
                              Expanded(
                                  child: Column(
                                children: <Widget>[
                                  Row(
                                    crossAxisAlignment:
                                        CrossAxisAlignment.center,
                                    children: <Widget>[
                                      Expanded(
                                        child: Text(
                                          "分享",
                                          overflow: TextOverflow.ellipsis,
                                          maxLines: 1,
                                        ),
                                      ),
                                      Container(
                                        width: 15.0,
                                        height: 15.0,
                                        child: Image.asset("images/arr.png"),
                                      ),
                                    ],
                                  ),
                                ],
                              )),
                              SizedBox(
                                width: 10.0,
                              )
                            ],
                          ),
                        ],
                      )),
                  onTap: () {
                    _getBatteryLevel();
                  },
                ),
                Container(
                  color: Color(0xffeeeeee),
                  height: 10.0,
                ),
              ],
            ),
          ),
        ),
        new SliverList(
          delegate: SliverChildBuilderDelegate((context, index) {
            return buildText(index);
          }, childCount: list.length),
        ),
      ],
    );
  }

//  FutureBuilder<List<Item>> buildFutureBuilder() {
//    return new FutureBuilder<List<Item>>(
//      builder: (context, AsyncSnapshot<List<Item>> async) {
//        //在这里根据快照的状态，返回相应的widget
//        if (async.connectionState == ConnectionState.active ||
//            async.connectionState == ConnectionState.waiting) {
//          return new Center(
//            child: new CircularProgressIndicator(),
//          );
//        }
//        if (async.connectionState == ConnectionState.done) {
//          debugPrint("done");
//          if (async.hasError) {
//            return new Center(
//              child: new Text("ERROR"),
//            );
//          } else if (async.hasData) {
//            List<Item> list = async.data;
//            return new RefreshIndicator(
//                child: buildListView(context, list),
//                onRefresh: refresh);
//          }
//        }
//      },
//      future: future,
//    );
//  }
//
}
